package org.rugby.online.persistency;

import static org.junit.Assert.*;

import java.util.Locale;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.rugby.online.core.exception.RboException;
import org.rugby.online.core.humans.RboProfession;
import org.rugby.online.core.log.RboLogger;
import org.rugby.online.impl.DefaultGamer;
import org.rugby.online.impl.DefaultNonPlayer;

public class DefaultPersistencyStrategyTest {

	private static RboPersistencyStragtegy strategy;
	
	private static DefaultGamer gamer;
	
	private static long gamerId;
	
	@BeforeClass
	public static void before() {
		gamer = new DefaultGamer("TEST");
		gamerId = gamer.getId();
		strategy = DefaultPersistencyStrategy.getInstance();
		try {
			strategy.init();
		} catch (RboException e) {
			RboLogger.parse(e);
		}
	}
	
	@Before
	public void setUp() throws Exception {
		
	}

	@Test
	public void testGetMainPersistencyPath() {
		assertTrue(strategy.getMainPersistencyPath().equals("./rso"));
	}

	@Test
	public void testGetPersistencyPathForClass() {
		assertNotNull(strategy.getPersistencyPathForClass(DefaultGamer.class));
		assertTrue(strategy.getPersistencyPathForClass(DefaultGamer.class).equals("./rso/gamers"));
	}

	@Test
	public void testGetPersistencyClassList() {
		assertNotNull(strategy.getPersistencyClassList());
		assertTrue(strategy.getPersistencyClassList().size() == DefaultPersistencyMapper.values().length);
	}

	@Test
	public void testWriteObject() {
		try {
			strategy.writeObject(DefaultGamer.class, gamerId, gamer);
		} catch (RboException e) {
			RboLogger.parse(e);
		}
	}

	@Test
	public void testGetObject() {
		try {
			DefaultGamer gamerTest = (DefaultGamer) strategy.getObject(DefaultGamer.class, gamerId);
			assertNotNull(gamerTest);
			assertEquals(gamer.getId(), gamerTest.getId());
			assertEquals(gamer.getName(), gamerTest.getName());
		} catch (RboException e) {
			RboLogger.parse(e);
		}
	}

	@Test
	public void testUpdateObject() {
		gamer.getStaffTeam().getStaff().add(new DefaultNonPlayer(RboProfession.MANAGEMENT_MAIN, Locale.FRENCH));
		try {
			strategy.updateObject(DefaultGamer.class, gamerId, gamer);
			DefaultGamer gamerTest = (DefaultGamer) strategy.getObject(DefaultGamer.class, gamerId);
			assertNotNull(gamerTest);
			assertEquals(gamer, gamerTest);
		} catch (RboException e) {
			RboLogger.parse(e);
		}
	}

	@Test
	public void testDeleteObject() {
		try {
			strategy.deleteObject(DefaultGamer.class, gamerId);
		} catch (RboException e) {
			RboLogger.parse(e);
		}
	}

}
